#include <stdlib.h>
#include <stdio.h>
#include "../tools/list.h"

#define TEST_MSG(num, status) { printf("Test %d %s\n", num, status); }
#define TEST(num) \
{ \
	if (test ## num()) { TEST_MSG(num, "FAILED"); } \
	else { TEST_MSG(num, "passed"); } \
}

int test1();
int test2();
int test3();
int test4();
int test5();

int main()
{
	TEST(1);
	TEST(2);
	TEST(3);
	TEST(4);
	TEST(5);
	printf("End of test\n");
	return 0;
}

int test1()
{
	/* construct and destruct */
	list_node_t* list_node = list_construction();
	list_destruct(list_node);
	return 0;
}

int test2()
{
	/*add and find*/
	list_node_t* list_node = list_construction();
	list_node = list_add(list_node, 1, 91);
	list_node = list_add(list_node, 2, 92);
	if (list_find(list_node, 1)->value != 91) return 1;
	if (list_find(list_node, 2)->key != 2) return 1;
	if (list_find(list_node, 3) != NULL) return 1;
	list_destruct(list_node);
	return 0;
}

int test3()
{
	/*del*/
	list_node_t* list_node = list_construction();
	list_node = list_add(list_node, 1, 91);
	list_node = list_add(list_node, 2, 92);
	list_node = list_node_del(list_find(list_node, 1));
	if (list_find(list_node, 2)->key != 2) return 1;
	if (list_find(list_node, 1) != NULL) return 1;
	list_destruct(list_node);	
	return 0;
}

int test4()
{
	/*next and prev*/
	list_node_t* list_node = list_construction();
	list_node = list_add(list_node, 1, 91);
	list_node = list_add(list_node, 2, 92);
	list_node = list_find(list_node, 1);
	list_node = list_next(list_node);
	if (list_node->value != 92) return 1;
	list_node = list_prev(list_node);
	if (list_node->value != 91) return 1;
	list_destruct(list_node);		
	return 0;
}

int test5()
{
	/*find in empty list*/
	list_node_t* list_node = list_construction();
	list_node = list_find(list_node, 91919);
	if (list_node != NULL) return 1;
	list_destruct(list_node);		
	return 0;
}
